<template>
  <div class="list-page">
    <Row>
      <div class="toolbar">
        <router-link :to="{ path: '/coupons/add' }">
          <Button
            type="primary"
            icon="md-add"
            style="margin: 0 10px"
            v-if="Validation.check(Resources.BANNERS_ADD)"
          >新增优惠
          </Button>
        </router-link>
        <Input
          placeholder="请输入优惠券名称"
          v-model="bloc.form.CouponName"
          style="width: 180px"
        />
        <DatePicker
          type="daterange"
          placeholder="添加时间"
          format="yyyy/MM/dd"
          v-model="bloc.form.date"
          :editable="false"
          style="width: 200px; margin-right: 10px"
        ></DatePicker>
        <Button
          type="primary"
          icon="ios-search"
          style="margin: 0 10px"
          size="default"
          @click="bloc.load(1, bloc.paged.pageSize)"
        >查询
        </Button>
        <Button
          icon="md-refresh"
          style="margin-right: 10px"
          size="default"
          @click="bloc.reset()"
        >重置</Button
        >
      </div>
    </Row>
    <Row>
      <Table
        border
        :loading="bloc.loading"
        :columns="columns"
        highlight-row
        :data="bloc.paged.items"
      >
        <template slot-scope="{ row }" slot="action">
          <div style="white-space: nowrap">
            <Button
              size="small"
              @click="modifyItem(row)"
              v-if="Validation.check(Resources.BANNERS_UPDATE) && bloc.logintype !== '2' && tepee!=='2' && row.state===0"
              type="info"
            >编辑
            </Button>
            <Button
              size="small"
              @click="banItem(row)"
              v-if="bloc.logintype !== '2' && row.state !== 2"
              type="warning"
            >作废
            </Button>
            <Button
              size="small"
              @click="toDetail(row.couponId)"
              v-if="row.state !== 2"
              type="info"
            >查看发放会员
            </Button>
<!--            <Button-->
<!--              size="small"-->
<!--              @click="removeItem(row)"-->
<!--              v-if="Validation.check(Resources.BANNERS_DELETE) && row.state === 2"-->
<!--              type="error"-->
<!--            >删除-->
<!--            </Button>-->
          </div>
        </template>
        <slot name="footer" slot="footer">
          <Page
            show-elevator
            show-total
            show-sizer
            :total="bloc.paged.itemCount"
            :current="bloc.paged.pageIndex"
            :page-size="bloc.paged.pageSize"
            :page-size-opts="[15, 30, 50, 100]"
            @on-change="(val) => bloc.load(val, bloc.paged.pageSize)"
            @on-page-size-change="(val) => bloc.load(bloc.paged.pageIndex, val)"
          />
        </slot>
      </Table>
    </Row>
  </div>
</template>

<script lang="ts">
    import {Component, Vue} from 'vue-property-decorator';
    import { Route } from 'vue-router';
    import { CouponsBloc } from '@/view/coupons/CouponsBloc';
    import { Coupons } from '@/base/model/Coupons';
    import { formatDate } from '@/base/util/Dates';
    import { Validation } from '@/base/Validation';
    import { Resources } from '@/base/Resources';
    import { store } from '@/store/Store';
    import {isEmpty} from '@/base/util/Strings';
    const columns = (bloc: CouponsBloc) => [
        {
            title: '优惠券名称',
            key: 'couponName',
            align: 'left',
            minWidth: 50,
            tooltip: true
        },
        {
            title: '有效期',
            key: 'dayNum',
            render: (h: any, params: any) => {
                return h('span', {
                    domProps: {
                        innerText: params.row.startTime
                            ? params.row.startTime.split(' ')[0] + '至' + bloc.getEndTime(params.row).toISOString().split('T')[0]// date
                            : params.row.daynum
                              ? params.row.daynum.toString() + '天'
                              : '无期限'
                    }
                });
            },
            align: 'left',
            minWidth: 200,
            tooltip: true
        },
        {
            title: '使用规则',
            render: (h: any, params: any) => {
                return h('span', {
                    domProps: {
                        innerText: '满' + params.row.condition + '减' + params.row.reduce
                    }
                });
            },
            align: 'left',
            minWidth: 50,
            tooltip: true
        },
        {
            title: '优惠券所属',
            render: (h: any, params: any) => {
                return h('span', {
                    domProps: {
                        innerText: params.row.agentId === 1
                            ? bloc.getCompanyName(params.row.companyId)
                            : bloc.getAgentName(params.row.agentId)
                    }
                });
            },
            align: 'left',
            minWidth: 220,
            tooltip: true
        },
        {
            title: '开启设置',
            key: 'state',
            render: (h: any, params: any) => {
                return h('div', [
                    params.row.state === 2 ? h('span', {
                            domProps: {
                                innerText: '已作废'
                            },
                            style: {
                                color: 'red'
                            }
                        }) : h(
                        'i-switch',
                        {
                            props: {
                                type: 'primary',
                                size: 'small',
                                title: '状态',
                                disabled: (bloc.compareTime(params.row) || params.row.state !== 0 || !Validation.check(Resources.BANNERS_UPDATE)),
                                value: params.row.state === 1
                            },
                            style: {
                                marginRight: '2px'
                            },
                            on: {
                                'on-change': value => {
                                    bloc.switchStatus(params.row.couponId, value);
                                }
                            }
                        },
                        'selected'
                    )
                ]);
            },
            tooltip: true,
            width: 80
        },
        {
            title: '创建时间',
            render: (h: any, params: any) => {
                return h('span', {
                    domProps: {
                        innerText: params.row.created
                    }
                });
            },
            width: 200,
            align: 'left',
            tooltip: true
        },
        {
            title: '操作',
            slot: 'action',
            width: 300,
            align: 'left',
            fixed: 'right'
        }
    ];

    @Component
    export default class extends Vue {
        beforeRouteEnter(to: Route, from: Route, next: (vm: any) => void) {
            next((vm) => {
                vm.bloc
                    .load(vm.bloc.paged.pageIndex, vm.bloc.paged.pageSize)
                    .catch((e) => {
                        vm.$Message.error('操作错误');
                    });
                vm.bloc.getCompanyList(vm.bloc.companyPage.pageIndex, vm.bloc.companyPage.pageSize, 'ASC');
                vm.bloc.getAgentList(vm.bloc.angent.pageIndex, vm.bloc.angent.pageSize, 'ASC');

                if (store.state.user.tepee === '2') { // 隐藏经销商栏目
                    this.columns = this.columns.filter(column => column.key !== 'agentName');
                }
            });
        }
        bloc: CouponsBloc = new CouponsBloc();
        Validation = Validation;
        Resources = Resources;
        columns = columns(this.bloc);

        public row: any;
        banItem(row) {
            this.row = row;
            this.$Modal.confirm({
                title: '是否确定作废此优惠券？',
                content: '已发送用户的优惠券也会作废，请谨慎操作！',
                okText: '确认',
                cancelText: '取消',
                onOk: () => {
                    this.bloc.removeItem(row, 1)
                        .then((res) => {
                            if (res.name === '') {
                                this.$Message.success('作废成功');
                            } else {
                                this.$Message.error('作废失败');
                            }
                        }).finally(() => {
                        this.bloc.reset();
                    });
                }
            });
        }

        removeItem(row) {
            this.row = row;
            this.$Modal.confirm({
                title: '是否确定删除此优惠券？',
                content: '此优惠券将不再显示，请谨慎操作！',
                okText: '确认',
                cancelText: '取消',
                onOk: () => {
                    this.bloc.removeItem(row, 0)
                        .then((res) => {
                            if (res.name === '') {
                                this.$Message.success('删除成功');
                            } else {
                                this.$Message.error('删除失败');
                            }
                        }).finally(() => {
                        this.bloc.reset();
                    });
                }
            });
        }
        modifyItem(dataItem: Coupons) {
            this.$router.push({
                path: '/coupons/edit/' + dataItem.couponId});
        }
        get tepee() {
            this.bloc.logintype = store.state.user.tepee;
            // if (store.state.user.tepee == '2') { // 隐藏经销商栏目
            //     this.columns = this.columns.filter(column => column.key !== 'agentName');
            // }
            return store.state.user.tepee
        }
        toDetail(id) {
            this.$router.push({
                path: '/coupons/member/' + id
            });
        }
    }
</script>

<style lang="less" scoped>
  .color1 {
    color: #ffa500;
  }
  .color2 {
    color: #3da5f5;
  }
</style>
